package ps.yj.y1j0.business;

import ps.yj.y1j0.data.YJDataObject;
import ps.yj.y1j0.data.YJOrderBy;
import ps.yj.y1j0.data.YJWhere;
import ps.yj.y1j0.data.database.YJDataManager;
import ps.yj.y1j0.data.database.YJDataManagerImpl;
import ps.yj.y1j0.data.database.YJSqlManager;
import ps.yj.y1j0.data.database.YJSqlManagerImpl;

import java.util.List;
import java.util.Map;

/**
 * @author yanjun
 */
public abstract class YJDataHandlerImpl implements YJDataHandler {
    protected YJSqlManager sqlManager = null;
    protected YJDataManager dataManager = null;
    protected String tableName = null;
    protected String idProperty = null;

    public YJDataHandlerImpl() {
        dataManager = YJDataManagerImpl.getInstance();
        sqlManager = YJSqlManagerImpl.getInstance();
    }

    @Override
    public int insert(YJDataObject data) {
        String[] properties = data.properties();
        int size = properties.length, i = 0;
        String sql = sqlManager.createInsert(tableName, idProperty, properties);
        Object[] values = new Object[size - 1];
        for (String property : properties) {
            if (property.equals(idProperty))
                continue;
            values[i++] = data.get(property);
        }
        return dataManager.updateTable(sql, values);
    }

    @Override
    public int update(YJDataObject data) {
        String[] properties = data.properties();
        int size = properties.length, i = 0;
        String sql = sqlManager.createUpdate(tableName, idProperty, properties);
        Object[] values = new Object[size];
        for (String property : properties) {
            if (property.equals(idProperty))
                continue;
            values[i++] = data.get(property);
        }
        values[size - 1] = data.get(idProperty);
        return dataManager.updateTable(sql, values);
    }

    @Override
    public int delete(Object[] idArray) {
        String sql = sqlManager.createDelete(tableName, idProperty, idArray.length);
        return dataManager.updateTable(sql, idArray);
    }

    @Override
    public List<Map<String, Object>> select(YJWhere[] whereArray, YJOrderBy[] orderByArray, String[] groupByArray) {
        String sql = "SELECT * FROM " + tableName;
        return select(sql, whereArray, orderByArray, groupByArray);
    }

    @Override
    public List<Map<String, Object>> select(String sql, YJWhere[] whereArray, YJOrderBy[] orderByArray, String[] groupByArray) {
        String selectSql = sqlManager.createSelect(sql, whereArray, orderByArray, groupByArray);
        Object[] values = null;
        if (whereArray != null) {
            values = new Object[whereArray.length];
            int i = 0, j = 0;
            for (YJWhere where : whereArray) {
                if (where.getAnd()) {
                    values[i++] = where.getValue();
                }
            }
            for (YJWhere where : whereArray) {
                if (!where.getAnd()) {
                    values[i + j++] = where.getValue();
                }
            }
        }
        return dataManager.queryTable(selectSql, values);
    }

    @Override
    public Boolean create(String[] properties) {
        String sql = sqlManager.createTable(tableName, idProperty, properties);
        return dataManager.updateTable(sql, new Object[]{}) == 0 ? true : false;
    }

//    @Override
//    public Boolean alert(YJDataObject data) {
//        String sql = sqlManager.alertTable(tableName,data.properties());
//        return dataManager.manageTable(sql);
//    }

    @Override
    public Boolean drop() {
        String sql = sqlManager.dropTable(tableName);
        return dataManager.updateTable(sql, new Object[]{}) == 0 ? true : false;
    }

    @Override
    public Boolean check() {
        String sql = sqlManager.checkTable(tableName);
        List result = dataManager.queryTable(sql, null);
        return (result.size() == 1 && ((Map<String, Object>) result.get(0)).get("num").toString().equals("1")) ? true : false;
    }

    protected String getTableName() {
        return tableName;
    }

    protected void setTableName(String tableName) {
        this.tableName = tableName;
    }

    protected String getIdProperty() {
        return idProperty;
    }

    protected void setIdProperty(String idProperty) {
        this.idProperty = idProperty;
    }

    protected YJSqlManager getSqlManager() {
        return sqlManager;
    }

    protected void setSqlManager(YJSqlManager sqlManager) {
        this.sqlManager = sqlManager;
    }
}
